package finalproject.view.parser;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import finalproject.controller.commands.Command;
import finalproject.model.player.Player;


/**
 * This class is part of the "World of Zuul" application. "World of Zuul" is a
 * very simple, text based adventure game.
 * 
 * This parser reads user input and tries to interpret it as an "Adventure"
 * command. Every time it is called it reads a line from the terminal and tries
 * to interpret the line as a two-word command. It returns the command as an
 * object of class Command.
 *
 * The parser has a set of known command words. It checks user input against the
 * known commands, and if the input is not one of the known commands, it returns
 * a command object that is marked as an unknown command.
 * 
 * @author Michael Kölling and David J. Barnes
 * @version 2011.08.10
 */
public class Parser {
	/**
	 * Create a parser to read from the terminal window.
	 */
	public Parser()
	{

	}

	/**
	 * @param player
	 * @return The next command from the user.
	 */
	public Command getCommand(Player player, String command)
	{
		String commandName = null;
		List<String> commandArgs = new ArrayList<>();

		// Find up to two words on the line.
		Scanner tokenizer = new Scanner(command);
		if (tokenizer.hasNext())
		{
			commandName = tokenizer.next(); // get first word
			while (tokenizer.hasNext())
				commandArgs.add(tokenizer.next()); // get next words
		}
		tokenizer.close();

		// Create and return the command
		return Command.getCommand(player, commandName, commandArgs.toArray(new String[0]));
	}
}
